package com.partner.boot.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.partner.boot.common.Result;
import com.partner.boot.controller.domain.LoginDTO;
import com.partner.boot.controller.domain.ResetPassRequest;
import com.partner.boot.controller.domain.UserRequest;
import com.partner.boot.entity.User;
import com.partner.boot.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "无权限接口列表")
@RestController
@Slf4j
public class WebController {

    @Autowired
    UserServiceImpl userService;

    @ApiOperation(value = "版本校验接口")
    @GetMapping(value = "/")
    public String version() {
        String ver = "partner-back-0.0.1-SNAPSHOT";  // 应用版本号
        Package aPackage = WebController.class.getPackage();
        String title = aPackage.getImplementationTitle();
        String version = aPackage.getImplementationVersion();
        if (title != null && version != null) {
            ver = String.join("-", title, version);
        }
        return ver;
    }

    @ApiOperation(value = "用户登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody UserRequest user) {
        LoginDTO dbUser = userService.login(user);
        return Result.success(dbUser);
    }

    @ApiOperation(value = "用户注册接口")
    @PostMapping("/register")
    public Result register(@RequestBody UserRequest user) {
        User dbUser = userService.register(user);
        return Result.success(dbUser);
    }

    @ApiOperation(value = "邮箱验证接口")

    @GetMapping("/email")
    public Result sendEmail(@RequestParam String email, @RequestParam String type) {
        userService.sendEmail(email,type);
        return Result.success();
    }

    @ApiOperation(value = "校验邮箱code接口")
    @GetMapping("/visResetPassEmailCode")
    public Result visResetPassEmailCode(@RequestParam String email, @RequestParam String emailCode) {
        userService.visResetPassEmailCode(email,emailCode);
        return Result.success();
    }

    @ApiOperation(value = "用户密码重置接口")
    @PutMapping("/password/reset")
    public Result resetPass(@RequestBody ResetPassRequest resetPassRequest) {
        userService.resetPass(resetPassRequest);
        return Result.success();
    }

    @ApiOperation(value = "随机生成中文")
    @GetMapping("/randomChineseName")
    public Result randomChineseName() {
        String randomName = userService.getRandomName(6);
        return Result.success(randomName);
    }

}
